﻿using UniversalPrinter;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;
using Microsoft.Win32;

namespace UniversalPrinter.Test
{
    [TestClass()]
    public class DocumentPrinterTest
    {

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        private string TestFilesDirectoryPath
        {
            get
            {
                return new System.IO.DirectoryInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).Parent.Parent.Parent.FullName.ToString() + @"\TestFiles\";
            }
        }

        [TestMethod]
        public void SavePrinterListTest()
        {
            string logPath = @"c:\PrinterList.txt";
            using (DocumentPrinter objPrint = new DocumentPrinter())
            {
                objPrint.SavePrinterList(logPath);
                Assert.IsTrue(System.IO.File.Exists(logPath), DocumentPrinterResult.UNSPECIFIED_ERROR.ToString());
            }
        }

        [TestMethod]
        public void PrintImageFile()
        {
            using (DocumentPrinter objPrint = new DocumentPrinter())
            {
                objPrint.Print(this.TestFilesDirectoryPath + "test.jpg");
                Assert.AreEqual(objPrint.PrintResult, DocumentPrinterResult.SENT_SUCCESSFULLY, objPrint.PrintResult.ToString());
            }
        }

        [TestMethod]
        public void PrinterWMIInformations()
        {
            using (DocumentPrinter objPrint = new DocumentPrinter())
            {
                objPrint.LogPrinterWMIInformations(@"C:\");
                Assert.IsTrue(System.IO.File.Exists(@"C:\Microsoft XPS Document Writer.txt"), DocumentPrinterResult.PRINTER_NOT_FOUND.ToString());
            }
        }

        [TestMethod]
        public void PrintFromTargetPrinter()
        {
            using (DocumentPrinter objPrint = new DocumentPrinter())
            {
                //set printer Windows DeviceId, type \\<IpAddress>\<PrinterName> for remote printers
                objPrint.Print(this.TestFilesDirectoryPath + "test.txt", @"Microsoft XPS Document Writer");
                Assert.AreEqual(objPrint.PrintResult, DocumentPrinterResult.SENT_SUCCESSFULLY, objPrint.PrintResult.ToString());
            }
        }

        [TestMethod]
        public void PrintPRNFileFromTargetPrinter()
        {
            using (DocumentPrinter objPrint = new DocumentPrinter())
            {
                objPrint.Print(this.TestFilesDirectoryPath + "test.prn", @"\\192.168.5.1\Samsung SCX-4x21 Series");
                Assert.AreEqual(objPrint.PrintResult, DocumentPrinterResult.SENT_SUCCESSFULLY, objPrint.PrintResult.ToString());
            }
        }

        [TestMethod]
        public void PrintFromDefaultPrinter()
        {
            using (DocumentPrinter objPrint = new DocumentPrinter())
            {
                objPrint.Print(this.TestFilesDirectoryPath + "test.txt");
                Assert.AreEqual(objPrint.PrintResult, DocumentPrinterResult.SENT_SUCCESSFULLY, objPrint.PrintResult.ToString());
            }
        }

        [TestMethod]
        public void PrintTestFiles()
        {
            using (DocumentPrinter objPrint = new DocumentPrinter())
            {
                foreach (System.IO.FileInfo file in new System.IO.DirectoryInfo(this.TestFilesDirectoryPath).GetFiles())
                {
                    if (file.Extension != ".doc" && file.Extension != ".docx")
                    {
                        objPrint.Print(file.FullName);
                        Assert.AreEqual(objPrint.PrintResult, DocumentPrinterResult.SENT_SUCCESSFULLY, objPrint.PrintResult.ToString() + ", " + file.FullName);
                    }
                }
            }
        }
    }
}
